/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2021-03-17
 * Time: 22:47
 */

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
          , diagram_scene_(new DiagramScene(this))
          , diagram_view_(new DiagramView(diagram_scene_))
{
    InitUi();
    InitEvent();
    InitAction();
    InitMenu();
}

MainWindow::~MainWindow()
{

}

void MainWindow::InitUi()
{
    setCentralWidget(diagram_view_);

    //QVBoxLayout * main_layout = new QVBoxLayout(this);
    //main_layout->addWidget(diagram_view_);


    diagram_scene_->setSceneRect(QRectF(0, 0, 5000, 5000));


}

void MainWindow::InitEvent()
{
    /*[1]: 将View中的创建菜单和scene的创建类型进行关联，方便在scene中鼠标点击事件中进行创建 */
    connect(diagram_view_, &DiagramView::SignalRectItem, this,[=]() {
        diagram_scene_->SetSceneMode(DiagramScene::SceneMode::InsertRectItem);
    });
    connect(diagram_view_, &DiagramView::SignalCircleItem,this, [=]() {
        diagram_scene_->SetSceneMode(DiagramScene::SceneMode::InsertCircleItem);
    });
    connect(diagram_view_, &DiagramView::SignalDiamondItem, this,[=]() {
        diagram_scene_->SetSceneMode(DiagramScene::SceneMode::InsertDiamondItem);
    });
    connect(diagram_view_, &DiagramView::SignalStraightLineItem,this, [=]() {
        diagram_scene_->SetSceneMode(DiagramScene::SceneMode::InsertStraightLineItem);
    });

    connect(diagram_view_, &DiagramView::SignalRightAngleItem, this,[=]() {
        diagram_scene_->SetSceneMode(DiagramScene::SceneMode::InsertRightAngleLineItem);
    });

    connect(diagram_view_, &DiagramView::SignalDeleteItem, this,[=]() {

        for (QGraphicsItem *item : diagram_scene_->selectedItems()) {
            if (item->type() == DiagramBaseItem::Type) {
                auto temp_item = qgraphicsitem_cast<DiagramBaseItem *>(item);
                QString id = temp_item->data(DiagramBaseItem::IDData).toString();

                /*[1]: 先删除item 后删除包含该item ID的所有线段*/
                diagram_scene_->DeleteOneItem(temp_item);
                diagram_scene_->DeleteArrowByStartItemID(id);
            }
            else if (item->type() == Arrow::Type) {
                /*[1]: 直接删除线*/
                diagram_scene_->DeleteOneArrow(qgraphicsitem_cast<Arrow *>(item));
            }

        }
    });

    connect(diagram_view_, &DiagramView::SignalRotateItem, this,[=]() {
        for (auto item : diagram_scene_->selectedItems()) {
            if (item->type() == DiagramBaseItem::Type) {
                auto temp_item = qgraphicsitem_cast<DiagramBaseItem *>(item);

                QString id = temp_item->data(DiagramBaseItem::IDData).toString();
                temp_item->RotateItem();

                diagram_scene_->UpdateArrow(id);
            }
        }
    });

    connect(diagram_view_,&DiagramView::SignalItemAlgin, this,[=](){
        diagram_scene_->SlotAutoAlignItem();
    });
}

void MainWindow::InitAction()
{
    this->AddAction("New...", this, SLOT(SlotNewDiagram()), "Ctrl+N");
    this->AddAction("Open...", this, SLOT(SlotOpenDiagram()), "Ctrl+O");
    this->AddAction("Save", this, SLOT(SlotSaveDiagram()), "Ctrl+S");
    this->AddAction("Save As...", this, SLOT(SlotSaveDiagramAs()), "Ctrl+Shift+S");
    this->AddAction("Close", this, SLOT(SlotCloseDiagram()), "Ctrl+W");
}

void MainWindow::InitMenu()
{
    QList<QAction *> window_actions = this->actions();
    QMenu *menu;
    menu = menuBar()->addMenu("File");
    menu->addAction(window_actions[NewAction]);
    menu->addAction(window_actions[OpenAction]);
    menu->addSeparator();
    menu->addAction(window_actions[SaveAction]);
    menu->addAction(window_actions[SaveAsAction]);
    menu->addSeparator();
    menu->addAction(window_actions[CloseAction]);
}

bool MainWindow::SlotNewDiagram()
{
    return false;
}

bool MainWindow::SlotOpenDiagram()
{
    return false;
}

bool MainWindow::SlotSaveDiagram()
{
    return false;
}

bool MainWindow::SlotSaveDiagramAs()
{
    return false;
}

bool MainWindow::SlotCloseDiagram()
{
    return false;
}

bool MainWindow::SlotLoadDiagram(const QString &filePath)
{
    return false;
}

QAction *MainWindow::AddAction(const QString &text, QObject *slotObj, const char *slotFunction, const QString &shortcut)
{
    auto action = new QAction(text, this);
    connect(action, SIGNAL(triggered()), slotObj, slotFunction);

    if (!shortcut.isEmpty()) action->setShortcut(QKeySequence(shortcut));

    QMainWindow::addAction(action);
    return action;
}



